package com.wildwolf.project.wildwolf.ui;

import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Presence.Type;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.wildwolf.project.wildwolf.WildWolfService;
import com.wildwolf.project.wildwolf.R;
import com.wildwolf.project.wildwolf.service.Contact;
import com.wildwolf.project.wildwolf.service.PresenceAdapter;
import com.wildwolf.project.wildwolf.service.aidl.IXmppFacade;
import com.wildwolf.project.wildwolf.utils.WildWolfBroadcastReceiver;

/**
 * 处理好有请求页面
 * 
 * @author LEON
 * 
 */
public class SubscriptionActivity extends Activity implements OnClickListener {
	/**
	 * 日志Tag
	 */
	private static final String TAG = SubscriptionActivity.class.getSimpleName();
	/**
	 * 服务对象
	 */
	private static final Intent SERVICE_INTENT = new Intent();

	private IXmppFacade mService;

	private String mContact;

	private ServiceConnection mServConn = new WildwolfServiceConnection();

	private final WildWolfBroadcastReceiver mReceiver = new WildWolfBroadcastReceiver();

	static {
		SERVICE_INTENT.setComponent(new ComponentName("com.wildwolf.project.wildwolf",	"com.wildwolf.project.wildwolf.WildWolfService"));
	}

	/**
	 * Constructor.
	 */
	public SubscriptionActivity() {
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.subscription);
		findViewById(R.id.SubscriptionAccept).setOnClickListener(this);
		findViewById(R.id.SubscriptionRefuse).setOnClickListener(this);
		Contact c = new Contact(getIntent().getData());
		mContact = c.getJID();
		TextView tv = (TextView) findViewById(R.id.SubscriptionText);
		String str = String.format(getString(R.string.SubscriptText), mContact);
		
		tv.setText(str);
		
		this.registerReceiver(mReceiver, new IntentFilter(WildWolfBroadcastReceiver.WILDWOLF_CONNECTION_CLOSED));
	}

	@Override
	protected void onResume() {
		super.onResume();
		bindService(new Intent(this, WildWolfService.class), mServConn,BIND_AUTO_CREATE);
	}

	@Override
	protected void onPause() {
		super.onPause();
		unbindService(mServConn);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		this.unregisterReceiver(mReceiver);
	}

	/**
	 * 发送节点流
	 * 
	 * @param p
	 */
	private void sendPresence(Presence p) {
		PresenceAdapter preAdapt = new PresenceAdapter(p);
		try {
			mService.sendPresencePacket(preAdapt);
		} catch (RemoteException e) {
			Log.e(TAG, "Error while sending subscription response", e);
		}
	}

	/**
	 * 连接到当前的会话
	 */
	private class WildwolfServiceConnection implements ServiceConnection {

		/**
		 * Constructor.
		 */
		public WildwolfServiceConnection() {
		}

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			mService = IXmppFacade.Stub.asInterface(service);
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			mService = null;
		}
	}

	@Override
	public void onClick(View v) {

		Presence presence = null;
		switch (v.getId()) {
		case R.id.SubscriptionAccept:
			presence = new Presence(Type.subscribed);
			Toast.makeText(SubscriptionActivity.this,
					getString(R.string.SubscriptAccept), Toast.LENGTH_SHORT)
					.show();
			break;
		case R.id.SubscriptionRefuse:
			presence = new Presence(Type.unsubscribed);
			Toast.makeText(SubscriptionActivity.this,
					getString(R.string.SubscriptRefused), Toast.LENGTH_SHORT)
					.show();
			break;
		default:
			Toast.makeText(SubscriptionActivity.this,
					getString(R.string.SubscriptError), Toast.LENGTH_SHORT)
					.show();
		}
		if (presence != null) {
			presence.setTo(mContact);
			sendPresence(presence);
		}
		finish();

	}
}
